package com.dengxian.usercenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dengxian.usercenter.annotation.AuthCheck;
import com.dengxian.usercenter.common.BaseResponse;
import com.dengxian.usercenter.common.ErrorCode;
import com.dengxian.usercenter.common.ResultUtils;
import com.dengxian.usercenter.constant.CommonConstant;
import com.dengxian.usercenter.exception.BusinessException;
import com.dengxian.usercenter.model.domain.Images;
import com.dengxian.usercenter.model.domain.User;
import com.dengxian.usercenter.model.dto.DeleteRequest;
import com.dengxian.usercenter.model.dto.ImagesAddRequest;
import com.dengxian.usercenter.model.dto.ImagesQueryRequest;
import com.dengxian.usercenter.model.dto.ImagesUpdateRequest;
import com.dengxian.usercenter.service.ImagesService;
import com.dengxian.usercenter.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 图片接口
 */
@RestController
@RequestMapping("/images")
@Slf4j
public class ImagesController {
    @Resource
    private ImagesService imagesService;

    @Resource
    private UserService userService;

    /**
     * 创建
     * @param imagesAddRequest
     * @param request
     * @return
     */
    @AuthCheck(mustRole = "admin")
    @PostMapping("/add")
    public BaseResponse<Long> addImage(@RequestBody ImagesAddRequest imagesAddRequest, HttpServletRequest request) {
        if(imagesAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Images images = new Images();
        BeanUtils.copyProperties(imagesAddRequest, images);
        User loginUser = userService.getLoginUser(request);
        images.setUserId(loginUser.getId());
        boolean result = imagesService.save(images);
        if (!result) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        long newimagesId = images.getId();
        return ResultUtils.success(newimagesId);
    }

    /**
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @AuthCheck(mustRole = "admin")
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteImage(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long id = deleteRequest.getId();
        // 判断是否存在
        Images oldimages = imagesService.getById(id);
        if (oldimages == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        boolean b = imagesService.removeById(id);
        return ResultUtils.success(b);
    }

    /**
     * 更新
     *
     * @param imagesUpdateRequest
     * @param request
     * @return
     */
    @AuthCheck(mustRole = "admin")
    @PostMapping("/update")
    public BaseResponse<Boolean> updateImage(@RequestBody ImagesUpdateRequest imagesUpdateRequest, HttpServletRequest request) {
        if (imagesUpdateRequest == null ||  imagesUpdateRequest.getId() <= 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Images images = new Images();
        BeanUtils.copyProperties(imagesUpdateRequest, images);
        long id =imagesUpdateRequest.getId();
        // 判断是否存在
        Images oldimages = imagesService.getById(id);
        if (oldimages == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        boolean result = imagesService.updateById(images);
        return ResultUtils.success(result);
    }

    /**
     * 根据 id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<Images> getImageById(long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Images images = imagesService.getById(id);
        return ResultUtils.success(images);
    }

    /**
     * 获取列表
     *
     * @param imagesQueryRequest
     * @return
     */
    @GetMapping("/list")
    public BaseResponse<List<Images>> listImage(ImagesQueryRequest imagesQueryRequest) {
        Images imagesQuery = new Images();
        if (imagesQueryRequest != null) {
            BeanUtils.copyProperties(imagesQueryRequest, imagesQuery);
        }
        QueryWrapper<Images> queryWrapper = new QueryWrapper<>(imagesQuery);
        List<Images> imagesList = imagesService.list(queryWrapper);
        return ResultUtils.success(imagesList);
    }

    /**
     * 分页获取列表
     *
     * @param imagesQueryRequest
     * @return
     */
    @GetMapping("/list/page")
    public BaseResponse<Page<Images>> listImageByPage(ImagesQueryRequest imagesQueryRequest) {
        if (imagesQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Images imagesQuery = new Images();
        BeanUtils.copyProperties(imagesQueryRequest, imagesQuery);
        long current = imagesQueryRequest.getCurrent();
        long size = imagesQueryRequest.getPageSize();
        String sortField = imagesQueryRequest.getSortField();
        String sortOrder = imagesQueryRequest.getSortOrder();
        // 限制爬虫
//        if (size > 50) {
//            throw new BusinessException(ErrorCode.PARAMS_ERROR);
//        }
        QueryWrapper<Images> queryWrapper = new QueryWrapper<>(imagesQuery);
        queryWrapper.orderBy(StringUtils.isNotBlank(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);
        Page<Images> imagesPage = imagesService.page(new Page<>(current, size), queryWrapper);
        return ResultUtils.success(imagesPage);
    }
}
